import ElementUI from "element-ui";
import VueRouter from 'vue-router'
import AccountSettings from '../components/PersonData/AccountSettings'
import HonorLevel from '../components/PersonData/HonorLevel'
import PrivacySettings from '../components/PersonData/PrivacySettings'
import PersonalData from '../components/PersonData/PersonalData'
import Login from "../pages/login/Login.vue"
import WriteData from "../pages/PersonalData/WriteData.vue"
import Register from "../pages/login/Register.vue"
import ForgetPwd from "../pages/login/ForgetPassword.vue"
import WriteBlog from "../pages/blog/WriteBlog.vue"
import ManageLogin from '../components/LoginComponents/ManageLogin.vue'
import UserLogin from '../components/LoginComponents/UserLogin.vue'
import CreativeCenter from "../pages/createCenter/CreativeCenter"
import ContentManagement from "../components/CreativeCenter/ContentManagement"
import Drafts from "../components/ContentManagement/Drafts"
import PersonalCenter from "../pages/personalCenter/PersonalCenter"
import BlogDetails from '../pages/blog/BlogDetails.vue'
import HomePage from '../pages/homePage/HomePage.vue'
import BlogQuery from '../pages/blog/BlogQuery.vue'
import Comments from '../pages/blog/Comments.vue'
import AllArticles from '../components/ContentManagement/AllArticles'
import RecycleBin from '../components/ContentManagement/RecycleBin'
import RoleApply from '../components/ContentManagement/RoleApply'
import Audit from '../components/ContentManagement/Audit'
import PrivacyAgreement from "../pages/agreement/PrivacyAgreement"
import UserAgreement from "../pages/agreement/UserAgreement"
import UserPermissions from '../pages/UserPermissions/UserPermissions.vue'
import ExcellentBlogsRule from '../components/Permissions/ExcellentBlogsRule.vue'
import StarBlogsRule from '../components/Permissions/StarBlogsRule.vue'
import ExcellentBlogsListChoose from '../components/Permissions/ExcellentBlogsListChoose.vue'
import ExcellentBlogsList from '../components/Permissions/ExcellentBlogList.vue'
import StarBlogsList from '../components/Permissions/StarBlogsList.vue'
import StarBlogsListChoose from '../components/Permissions/StarBlogsListChoose.vue'
import UserBlogReview from '../components/Permissions/UserBlogReview.vue'
import BackgroundManage from '../pages/administrator/BackgroundManage.vue'
import RoleApplyList from '../components/AdminComponents/RoleApplyList'
import CommentsManagement from '../components/CommentsManagement/CommentsManagement'
import BlogReview from '../components/AdminComponents/BlogReview.vue'
import AssignRoles from '../components/AdminComponents/AssignRoles.vue'
import UserManagement from '../components/AdminComponents/UserManagement.vue'
import RoleManagement from '../components/AdminComponents/RoleManagement.vue'
import AllArticle from '../components/PersoanlCenter/AllArticle.vue'
import BrowsingHistory from '../components/PersoanlCenter/BrowsingHistory.vue'
import Collection from '../components/PersoanlCenter/Collection.vue'
import AttentionList from "../components/PersoanlCenter/AttentionList"
import AllVisible from "../components/ContentManagement/AllVisible"
import OnlyVisible from "../components/ContentManagement/OnlyVisible"
import FansList from "../components/PersoanlCenter/FansList"
import Message from '../pages/message/Message'
import SytemInforms from "../components/Message/SytemInforms"




const router = new VueRouter({
    routes: [{
            path: '/',
            redirect() {
                return '/login/UserLogin'
            }
        },
        //博客账号协议
        {
            name: 'UserAgreement',
            path: '/UserAgreement',
            component: UserAgreement
        },
        //用户隐私协议
        {
            name: 'PrivacyAgreement',
            path: '/PrivacyAgreement',
            component: PrivacyAgreement
        },
        //消息页面

        {
            name: 'Message',
            path: '/Message',
            component: Message,
            redirect: '/SytemInforms',
            children: [{
                name: 'SytemInforms',
                path: '/SytemInforms',
                component: SytemInforms
            }]
        },
        //登录
        {
            path: '/Login',
            components: {
                default: Login
            },
            name: "Login",
            children: [{
                    path: '/Login',
                    redirect: '/UserLogin'
                },
                {
                    // 用户登录
                    path: '/Login/UserLogin',
                    component: UserLogin,
                    name: 'UserLogin'
                },
                {
                    // 用户登录
                    path: '/Login/ManageLogin',
                    component: ManageLogin,
                    name: 'ManageLogin'
                },
            ]

        },
        //博客编辑
        {
            path: '/WriteBlog',
            component: WriteBlog,
            name: "WriteBlog",
            meta: {
                isAuth: true
            },
        },

        // 评选博客
        {
            path: '/UserPermissions',
            component: UserPermissions,
            name: "UserPermissions",
            children: [{
                    // 优秀博客规则
                    path: '/ExcellentBlogsRule',
                    component: ExcellentBlogsRule,
                    name: 'ExcellentBlogsRule',
                    // 路由拦截，没有权限的不能进入该页面
                    meta: {
                        isAuth: true,
                        role: 1
                    },
                    beforeEnter: (to, from, next) => {
                        // 角色权限拦截函数判断是否拥有角色权限并放行
                        PermissionIntercept(to, from, next)
                    }
                },
                {
                    // 博客之星规则
                    path: '/StarBlogsRule',
                    component: StarBlogsRule,
                    name: 'StarBlogsRule',
                    // 路由拦截，没有权限的不能进入该页面
                    meta: {
                        isAuth: true,
                        role: 2
                    },
                    beforeEnter: (to, from, next) => {
                        // 角色权限拦截函数判断是否拥有角色权限并放行
                        PermissionIntercept(to, from, next)
                    }
                },
                {
                    // 选择优秀博客
                    path: '/ExcellentBlogsListChoose',
                    component: ExcellentBlogsListChoose,
                    name: 'ExcellentBlogsListChoose',
                    // 路由拦截，没有权限的不能进入该页面
                    meta: {
                        isAuth: true,
                        role: 1
                    },
                    beforeEnter: (to, from, next) => {
                        // 角色权限拦截函数判断是否拥有角色权限并放行
                        PermissionIntercept(to, from, next)
                    }
                },
                // {
                //     // 选择优秀博客
                //     path: '/ExcellentBlogsListChoose',
                //     component: ExcellentBlogsListChoose,
                //     name: 'ExcellentBlogsListChoose'
                // },
                {
                    // 优秀博客
                    path: '/ExcellentBlogsList',
                    component: ExcellentBlogsList,
                    name: 'ExcellentBlogsList',
                    // 路由拦截，没有权限的不能进入该页面
                    meta: {
                        isAuth: true,
                        role: 1
                    },
                    beforeEnter: (to, from, next) => {
                        // 角色权限拦截函数判断是否拥有角色权限并放行
                        PermissionIntercept(to, from, next)
                    }
                },

                {
                    // 选择博客之星
                    path: '/StarBlogsListChoose',
                    component: StarBlogsListChoose,
                    name: 'StarBlogsListChoose',
                    // 路由拦截，没有权限的不能进入该页面
                    meta: {
                        isAuth: true,
                        role: 2
                    },
                    beforeEnter: (to, from, next) => {
                        // 角色权限拦截函数判断是否拥有角色权限并放行
                        PermissionIntercept(to, from, next)
                    }
                },

                // {
                //     // 选择博客之星
                //     path: '/starBlogsListChoose',
                //     component: starBlogsListChoose,
                //     name: 'starBlogsListChoose'
                // },
                {
                    // 博客之星
                    path: '/StarBlogsList',
                    component: StarBlogsList,
                    name: 'StarBlogsList',
                    // 路由拦截，没有权限的不能进入该页面
                    meta: {
                        isAuth: true,
                        role: 2
                    },
                    beforeEnter: (to, from, next) => {
                        // 角色权限拦截函数判断是否拥有角色权限并放行
                        PermissionIntercept(to, from, next)
                    }
                },

                // 内容审核
                {
                    path: '/UserBlogReview',
                    component: UserBlogReview,
                    name: 'UserBlogReview',
                    // 路由拦截，没有权限的不能进入该页面
                    meta: {
                        isAuth: true,
                        role: 3
                    },
                    beforeEnter: (to, from, next) => {
                        // 角色权限拦截函数判断是否拥有角色权限并放行
                        PermissionIntercept(to, from, next)
                    }
                },
            ]

        },




        //忘记密码
        {
            path: '/ForgetPwd',
            component: ForgetPwd,
            name: "ForgetPwd"
        },
        //注册

        {
            path: '/Register',
            component: Register,
            name: "Register"
        },
        //编写资料
        {
            path: '/WriteData',
            component: WriteData,
            name: 'WriteData',
            redirect: '/PersonalData',
            children: [{
                    //个人资料
                    path: '/PersonalData',
                    component: PersonalData,
                    name: 'PersonalData'
                }, {
                    //账号设置
                    path: 'accountSettings',
                    component: AccountSettings,
                    name: 'AccountSettings'
                },
                {
                    //荣誉等级
                    path: 'honorLevel',
                    component: HonorLevel,
                    name: 'HonorLevel'
                },
                {
                    //隐私设置
                    path: 'privacySettings',
                    component: PrivacySettings,
                    name: 'PrivacySettings'
                },
            ]
        },
        //创作中心
        {
            path: '/CreativeCenter',
            name: 'CreativeCenter',
            component: CreativeCenter,
            redirect: '/ContentManagement',
            meta: {
                isAuth: true,
            },
            children: [
                //内容管理
                {
                    path: '/ContentManagement',
                    component: ContentManagement,
                    name: 'ContentManagement',
                    redirect: '/AllArticles',
                    meta: {
                        isAuth: true,
                    },
                    children: [
                        //草稿箱
                        {
                            name: 'Drafts',
                            path: '/Drafts',
                            component: Drafts

                        },
                        //全部内容
                        {
                            path: '/AllArticles',
                            component: AllArticles,
                            name: 'AllArticles',
                            meta: {
                                isAuth: true,
                            },
                        },
                        //回收站
                        {
                            path: '/RecycleBin',
                            component: RecycleBin,
                            name: 'RecycleBin'
                        },
                        //审核中
                        {
                            path: '/Audit',
                            component: Audit,
                            name: 'Audit'
                        },
                        //全部可见
                        {
                            path: '/AllVisible',
                            component: AllVisible,
                            name: 'AllVisible'
                        },
                        //仅我可见
                        {
                            path: '/OnlyVisible',
                            component: OnlyVisible,
                            name: 'OnlyVisible'
                        }
                    ]
                },
                // 评论管理
                {
                    name: 'CommentsManagement',
                    path: '/CommentsManagement',
                    component: CommentsManagement
                },
                //角色申请
                {
                    name: 'RoleApply',
                    path: '/RoleApply',
                    component: RoleApply
                }
            ]

        },
        //个人主页
        {
            path: '/PersonalCenter',
            component: PersonalCenter,
            name: 'PersonalCenter',
            redirect: '/AllArticle',
            meta: {
                isAuth: true,
            },
            children: [
                //全部博客
                {
                    path: '/AllArticle',
                    component: AllArticle,
                    name: 'AllArticle',
                },
                //收藏
                {
                    path: '/Collection',
                    component: Collection,
                    name: 'Collection'
                },
                //浏览历史
                {
                    path: '/BrowsingHistory',
                    component: BrowsingHistory,
                    name: 'BrowsingHistory'
                },
                //关注列表
                {
                    path: '/AttentionList',
                    name: 'AttentionList',
                    component: AttentionList
                },
                //粉丝列表
                {
                    path: '/FansList',
                    name: 'FansList',
                    component: FansList
                }
            ]
        },
        //博客详细信息展示
        {
            path: '/Blogdetails',
            component: BlogDetails,
            name: 'BlogDetails',
            redirect: '/Comments',
            children: [{
                //博客评论
                path: '/Comments',
                component: Comments,
                name: 'Comments',
            }, ]
        },
        //博客主页
        {
            path: '/Homepage',
            component: HomePage,
            name: 'HomePage',

        },
        //博客查询
        {
            path: '/Blogquery',
            component: BlogQuery,
            name: 'BlogQuery'
        },
        //管理后台
        {
            path: '/BackgroundManage',
            component: BackgroundManage,
            name: 'BackgroundManage',
            children: [
                // 角色申请
                {
                    name: 'RoleApplyList',
                    path: '/RoleApplyList',
                    component: RoleApplyList
                },
                // 内容审核
                {
                    path: '/BlogReview',
                    component: BlogReview,
                    name: 'BlogReview',

                },
                // 分配角色
                {
                    path: '/AssignRoles',
                    component: AssignRoles,
                    name: 'AssignRoles',
                },
                // 用户管理
                {
                    path: '/UserManagement',
                    component: UserManagement,
                    name: 'UserManagement',
                },
                // 角色管理
                {
                    path: '/RoleManagement',
                    component: RoleManagement,
                    name: 'RoleManagement',
                },
            ]
        },





    ]
})

router.beforeEach((to, from, next) => {
    /*
    to:去往哪里
    from：从哪里来
    next：放行
    */
    // console.log("前置路由守卫", to, from);
    // window.console.log(router.options.routes[1].component.data);
    let token = JSON.parse(sessionStorage.getItem('LoginToken')) || ''
    if (to.meta.isAuth) { //判断是否需要鉴权
        if (!token.token) {
            alert("未登陆,请先登录!");

        } else {

            next()
            // console.log("我是token", token.token);
        }

    } else {
        next()
    }

})
// 角色权限拦截函数
function PermissionIntercept(to, from, next) {
    // 取出权限数组
    let permissions = JSON.parse(sessionStorage.getItem("role")).roles
    // 判断当前用户是否拥有该权限 有时为true没有则为false
    let role_flag = false
    // 判断是否有权限
    if (permissions) {
        for (var i = 0; i < permissions.role.length; i++) {
            if (permissions.role[i] === to.meta.role) {
                next() //放行
                role_flag = true //该用户拥有该权限
                break; //查出拥有权限时，跳出判断循环
            }
        }
    }
    // 当没有权限时弹出弹框提示
    if (!role_flag) {
        ElementUI.MessageBox.confirm("没有权限访问", "警告", {
            confirmButtonText: "确定",
            showCancelButton: false,
            type: "warning"
        }).then(() => {
            // ticket.end();
            localStorage.clear();
            window.location.href = window.location.origin;
        }).catch(() => {});
    }
}
//防止重复点击报错
const VueRouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(to) {
    return VueRouterPush.call(this, to).catch(err => err)
}
export default router